package full

import (
	"fmt"
	"db2s/ETL"
	"db2s/dbPar"
	"db2s/global"
	"db2s/parDef"
	"db2s/ref"
)

func (fu Full) rowsEr(drive string) (ter RowsEr, err error) {
	if ter, err = NewFullRows(drive); err != nil {
		err = ref.ErrAddPrintf("Full.rowsEr", err)
		return
	}
	return
}
func (fu Full) prefix() (meta any, err error) {
	var ter RowsEr
	if meta, err = parDef.CallMethod(fu.Drive, fu.Join, func(k int, v string) (result global.Return, err error) {
		if ter, err = fu.rowsEr(v); err != nil {
			return
		}
		if result, err = ter.Prefix(parDef.CtxBackground(parDef.GeneralReadMethodParameter(fu.Parameter1[k], ExternalCallAnyToInt64Func, ETL.String))); err != nil {
			return
		}
		return
	}); err != nil {
		err = ref.ErrAddPrintf("execDML", err)
		return
	}
	return
}
func (fu Full) execDml() (meta any, err error) {
	var ter RowsEr
	if meta, err = parDef.CallMethod(fu.Drive, fu.Join, func(k int, v string) (result global.Return, err error) {
		if ter, err = fu.rowsEr(v); err != nil {
			return
		}
		if result, err = ter.ExecDML(parDef.CtxBackground(parDef.GeneralWriteMethodParameter(fu.Parameter1[k], ExternalCallAnyToInt64Func, ETL.String))); err != nil {
			return
		}
		return
	}); err != nil {
		err = ref.ErrAddPrintf("execDML", err)
		return
	}
	return
}
func (fu Full) SelectIdxCol() (meta any, err error) {
	var ter RowsEr
	if meta, err = parDef.CallMethod(fu.Drive, fu.Join, func(k int, v string) (result global.Return, err error) {
		if ter, err = fu.rowsEr(v); err != nil {
			return
		}
		if result, err = ter.SelectIdxCol(parDef.CtxBackground(parDef.GeneralReadMethodParameter(fu.Parameter1[k], ExternalCallAnyToInt64Func, ETL.String))); err != nil {
			return
		}
		return
	}); err != nil {
		err = ref.ErrAddPrintf("SelectIdxCol", err)
		return
	}
	return
}
func (fu Full) selectAllCol() (meta any, err error) {
	var ter RowsEr
	if meta, err = parDef.CallMethod(fu.Drive, fu.Join, func(k int, v string) (result global.Return, err error) {
		if ter, err = fu.rowsEr(v); err != nil {
			return
		}
		if result, err = ter.SelectAllCol(parDef.CtxBackground(parDef.GeneralReadMethodParameter(fu.Parameter1[k], ExternalCallAnyToInt64Func, ETL.String))); err != nil {
			return
		}
		return
	}); err != nil {
		err = ref.ErrAddPrintf("selectAllCol", err)
		return
	}
	return
}

func (fu Full) stringValueCompare() (meta any, err error) {
	var ter RowsEr
	if meta, err = parDef.CallMethod(fu.Drive, fu.Join, func(k int, v string) (result global.Return, err error) {
		if ter, err = fu.rowsEr(v); err != nil {
			return
		}
		if result, err = ter.StringValueCompare(parDef.CtxBackground(parDef.GeneralReadMethodParameter(fu.Parameter1[k], ExternalCallAnyToInt64Func, ETL.String))); err != nil {
			return
		}
		return
	}); err != nil {
		err = ref.ErrAddPrintf("indexPart", err)
		return
	}
	return
}
func (fu Full) indexPart() (meta any, err error) {
	var ter RowsEr
	if meta, err = parDef.CallMethod(fu.Drive, fu.Join, func(k int, v string) (result global.Return, err error) {
		if ter, err = fu.rowsEr(v); err != nil {
			return
		}
		if result, err = ter.IndexPart(parDef.CtxBackground(parDef.GeneralReadMethodParameter(fu.Parameter1[k], nil, ETL.Map))); err != nil {
			return
		}
		return
	}); err != nil {
		err = ref.ErrAddPrintf("indexPart", err)
		return
	}
	return
}
func (fu Full) selectRowsSql() (meta any, err error) {
	var ter RowsEr
	if meta, err = parDef.CallMethod(fu.Drive, fu.Join, func(k int, v string) (result global.Return, err error) {
		if ter, err = fu.rowsEr(v); err != nil {
			return
		}
		if result, err = ter.SelectRowsSql(parDef.CtxBackground(parDef.GeneralReadMethodParameter(fu.Parameter1[k], nil, ETL.String))); err != nil {
			return
		}
		return
	}); err != nil {
		err = ref.ErrAddPrintf("readData", err)
		return
	}
	return
}

func (fu Full) readData() (meta any, err error) {
	var ter RowsEr
	if meta, err = parDef.CallMethodRes(fu.Drive, fu.Join, func(k int, v string) (result global.Return, err error) {
		if ter, err = fu.rowsEr(v); err != nil {
			return
		}
		if result, err = ter.ReadData(parDef.CtxBackground(parDef.GeneralReadMethodParameter(fu.Parameter1[k], nil, ETL.GarthStringPointGarth))); err != nil {
			return
		}
		return
	}); err != nil {
		err = ref.ErrAddPrintf("readDataEr", err)
		return
	}
	return
}
func (fu Full) readMiss() (meta any, err error) {
	var ter RowsEr
	if meta, err = parDef.CallMethod(fu.Drive, fu.Join, func(k int, v string) (result global.Return, err error) {
		if ter, err = fu.rowsEr(v); err != nil {
			return
		}
		if result, err = ter.ReadMiss(parDef.CtxBackground(parDef.GeneralReadMethodParameter(fu.Parameter1[k], nil, ETL.FlowAny))); err != nil {
			return
		}
		return
	}); err != nil {
		fmt.Println("----------readMiss-error:", fu.Parameter1[0].Object.Schema, fu.Parameter1[0].Object.Table, err)
		err = ref.ErrAddPrintf("readMiss", err)
		return
	}
	return
}

func (fu Full) insertSql(db dbPar.GeneralInsertSql) (meta any, err error) {
	var ter RowsEr
	if meta, err = parDef.CallMethod(fu.Drive, fu.Join, func(k int, v string) (result global.Return, err error) {
		if ter, err = fu.rowsEr(v); err != nil {
			return
		}
		if result, err = ter.InsertSql(parDef.CtxBackground(db)); err != nil {
			return
		}
		return
	}); err != nil {
		err = ref.ErrAddPrintf("insertSql", err)
		return
	}
	return
}
func (fu Full) insert(db dbPar.ExecSql) (meta any, err error) {
	var ter RowsEr
	if meta, err = parDef.CallMethodRes(fu.Drive, fu.Join, func(k int, v string) (result global.Return, err error) {
		if ter, err = fu.rowsEr(v); err != nil {
			return
		}
		if result, err = ter.Insert(parDef.CtxBackground(db)); err != nil {
			return
		}
		return
	}); err != nil {
		err = ref.ErrAddPrintf("insert", err)
		return
	}
	return
}
func (fu Full) GetFullPrefixST() (result any, err error) {
	return fu.prefix()
}
func (fu Full) GetFullDMLST() (result any, err error) {
	return fu.execDml()
}
func (fu Full) GetFullSelectIdxColST() (result any, err error) {
	return fu.SelectIdxCol()
}
func (fu Full) GetFullSelectAllColST() (result any, err error) {
	return fu.selectAllCol()
}
func (fu Full) GetFullStringValueCompare() (result any, err error) { return fu.stringValueCompare() }
func (fu Full) GetFullIndexPartST() (result any, err error)        { return fu.indexPart() }
func (fu Full) GetFullReadMissST() (result any, err error) {
	return fu.readMiss()
}
func (fu Full) GetFullSelectRowsSqlS() (result any, err error) { return fu.selectRowsSql() }
func (fu Full) GetFullReadDataST() (result any, err error) {
	return fu.readData()
}
func (fu Full) GetFullInsertSqlST() (result any, err error) {
	return fu.insertSql(dbPar.GeneralInsertSql{
		Schema:       fu.Parameter1[0].Object.Schema,
		Table:        fu.Parameter1[0].Object.Table,
		QueryColumn:  fu.Parameter1[0].ExecInsert.QueryCol,
		InsertColumn: fu.Parameter1[0].ExecInsert.Columns,
		InsertValue:  fu.Parameter1[0].Meta,
		LockCol:      fu.Parameter1[0].Object.LockCol,
		Prefix:       fu.Parameter1[0].ExecInsert.Prefix,
	})
}
func (fu Full) GetFullInsertST(parameter parDef.Parameter) (result any, err error) {
	return fu.insert(dbPar.ExecSql{
		DbDrive: ETL.WriteOpt{
			DB:        parameter.Con,
			SqlMode:   parameter.Options.SqlMode,
			SqlLogBin: parameter.Options.SqlLogBin,
		},
		Prefix: parameter.ExecInsert.Prefix,
		Sql:    parameter.Meta,
	})
}
